package com.hua.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hua.lease.model.entity.*;
import com.hua.lease.model.enums.ItemType;
import com.hua.lease.web.admin.mapper.*;
import com.hua.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.lease.web.admin.vo.attr.AttrValueVo;
import com.hua.lease.web.admin.vo.graph.GraphVo;
import com.hua.lease.web.admin.vo.room.RoomDetailVo;
import com.hua.lease.web.admin.vo.room.RoomItemVo;
import com.hua.lease.web.admin.vo.room.RoomQueryVo;
import com.hua.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

	@Autowired
	private GraphInfoService graphInfoService;

	@Autowired
	private RoomAttrValueService roomAttrValueService;

	@Autowired
	private RoomFacilityService roomFacilityService;

	@Autowired
	private RoomLabelService roomLabelService;

	@Autowired
	private RoomPaymentTypeService roomPaymentTypeService;

	@Autowired
	private RoomLeaseTermService roomLeaseTermService;

	@Autowired
	private RoomInfoMapper roomInfoMapper;

	@Autowired
	ApartmentInfoMapper apartmentInfoMapper;

	@Autowired
	private GraphInfoMapper graphInfoMapper;

	@Autowired
	private AttrValueMapper attrValueMapper;

	@Autowired
	private FacilityInfoMapper facilityInfoMapper;

	@Autowired
	private LabelInfoMapper labelInfoMapper;

	@Autowired
	private PaymentTypeMapper paymentTypeMapper;

	@Autowired
	private LeaseTermMapper leaseTermMapper;

	@Override
	public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
		boolean isUpdate = roomSubmitVo.getId() != null;
		super.saveOrUpdate(roomSubmitVo);

		//若为更新操作，则先删除与Room相关的各项信息列表
		if (isUpdate) {
			//1.删除原有graphInfoList
			LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
			graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
			graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
			graphInfoService.remove(graphQueryWrapper);

			//2.删除原有roomAttrValueList
			LambdaQueryWrapper<RoomAttrValue> attrQueryMapper = new LambdaQueryWrapper<>();
			attrQueryMapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
			roomAttrValueService.remove(attrQueryMapper);

			//3.删除原有roomFacilityList
			LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
			facilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
			roomFacilityService.remove(facilityQueryWrapper);

			//4.删除原有roomLabelList
			LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
			labelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
			roomLabelService.remove(labelQueryWrapper);

			//5.删除原有paymentTypeList
			LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
			paymentQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
			roomPaymentTypeService.remove(paymentQueryWrapper);


			//6.删除原有leaseTermList
			LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
			termQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
			roomLeaseTermService.remove(termQueryWrapper);
		}

		//1.保存新的graphInfoList
		List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
		if (!CollectionUtils.isEmpty(graphVoList)) {
			ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
			for (GraphVo graphVo : graphVoList) {
				GraphInfo graphInfo = new GraphInfo();
				graphInfo.setItemType(ItemType.ROOM);
				graphInfo.setItemId(roomSubmitVo.getId());
				graphInfo.setName(graphVo.getName());
				graphInfo.setUrl(graphVo.getUrl());
				graphInfoList.add(graphInfo);
			}
			graphInfoService.saveBatch(graphInfoList);
		}

		//2.保存新的roomAttrValueList
		List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
		if (!CollectionUtils.isEmpty(attrValueIds)) {
			List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
			for (Long attrValueId : attrValueIds) {
				RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
				roomAttrValueList.add(roomAttrValue);
			}
			roomAttrValueService.saveBatch(roomAttrValueList);
		}

		//3.保存新的facilityInfoList
		List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
		if (!CollectionUtils.isEmpty(facilityInfoIds)) {
			List<RoomFacility> roomFacilityList = new ArrayList<>();
			for (Long facilityInfoId : facilityInfoIds) {
				RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
				roomFacilityList.add(roomFacility);
			}
			roomFacilityService.saveBatch(roomFacilityList);
		}

		//4.保存新的labelInfoList
		List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
		if (!CollectionUtils.isEmpty(labelInfoIds)) {
			ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
			for (Long labelInfoId : labelInfoIds) {
				RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
				roomLabelList.add(roomLabel);
			}
			roomLabelService.saveBatch(roomLabelList);
		}

		//5.保存新的paymentTypeList
		List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
		if (!CollectionUtils.isEmpty(paymentTypeIds)) {
			ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
			for (Long paymentTypeId : paymentTypeIds) {
				RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
				roomPaymentTypeList.add(roomPaymentType);
			}
			roomPaymentTypeService.saveBatch(roomPaymentTypeList);
		}

		//6.保存新的leaseTermList
		List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
		if (!CollectionUtils.isEmpty(leaseTermIds)) {
			ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
			for (Long leaseTermId : leaseTermIds) {
				RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
				roomLeaseTerms.add(roomLeaseTerm);
			}
			roomLeaseTermService.saveBatch(roomLeaseTerms);
		}
	}

	@Override
	public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
		return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
	}

	@Override
	public RoomDetailVo getRoomDetailById(Long id) {

		//1.查询RoomInfo
		RoomInfo roomInfo = roomInfoMapper.selectById(id);

		//2.查询所属公寓信息
		ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

		//3.查询graphInfoList
		List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);

		//4.查询attrValueList
		List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);

		//5.查询facilityInfoList
		List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

		//6.查询labelInfoList
		List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

		//7.查询paymentTypeList
		List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

		//8.查询leaseTermList
		List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);


		RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
		BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

		adminRoomDetailVo.setApartmentInfo(apartmentInfo);
		adminRoomDetailVo.setGraphVoList(graphVoList);
		adminRoomDetailVo.setAttrValueVoList(attrvalueVoList);
		adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
		adminRoomDetailVo.setLabelInfoList(labelInfoList);
		adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
		adminRoomDetailVo.setLeaseTermList(leaseTermList);

		return adminRoomDetailVo;
	}

	@Override
	public void removeRoomById(Long id) {
		//1.删除RoomInfo
		super.removeById(id);

		//2.删除graphInfoList
		LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
		graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
		graphQueryWrapper.eq(GraphInfo::getItemId, id);
		graphInfoService.remove(graphQueryWrapper);

		//3.删除attrValueList
		LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
		attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
		roomAttrValueService.remove(attrQueryWrapper);

		//4.删除facilityInfoList
		LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
		facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
		roomFacilityService.remove(facilityQueryWrapper);

		//5.删除labelInfoList
		LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
		labelQueryWrapper.eq(RoomLabel::getRoomId, id);
		roomLabelService.remove(labelQueryWrapper);

		//6.删除paymentTypeList
		LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
		paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
		roomPaymentTypeService.remove(paymentQueryWrapper);

		//7.删除leaseTermList
		LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
		termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
		roomLeaseTermService.remove(termQueryWrapper);
	}
}




